﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace ico2cur
{
    class IconPreview : Control
    {
        public IconPreview()
        {
            BackColor = Color.White;
            Size = new Size(100, 100);
            m_BlackPen = new Pen(Color.Black);
            m_RedPen = new Pen(Color.Red);
            m_GreenPen = new Pen(Color.Green);
            m_ActualPen = m_RedPen;

            m_Timer = new Timer();
            m_Timer.Interval = 500;
            m_Timer.Tick += M_Timer_Tick;
            m_Timer.Start();

            Paint += new PaintEventHandler(IconPreview_Paint);
            Click += IconPreview_Click1;

            m_PreviewIcon = null;
            m_x = 0;
            m_y = 0;

            Visible = true;
        }

        private void IconPreview_Click1(object sender, EventArgs e)
        {
            if (m_PreviewIcon == null)
                return;

            m_x = (int)Math.Floor((double)PointToClient(MousePosition).X / ((double)Width / (double)m_PreviewIcon.Width));
            m_y = (int)Math.Floor((double)PointToClient(MousePosition).Y / ((double)Height / (double)m_PreviewIcon.Height));

            Invalidate();
        }

        private void M_Timer_Tick(object sender, EventArgs e)
        {
            if (m_ActualPen == m_RedPen)
                m_ActualPen = m_GreenPen;
            else
                m_ActualPen = m_RedPen;
            
            if (m_PreviewIcon != null)
                Invalidate(
                        new Rectangle(
                            (int)Math.Round(((double)Width / (double)(m_PreviewIcon.Width)) * (double)m_x),
                            (int)Math.Round(((double)Height / (double)(m_PreviewIcon.Height)) * (double)m_y),
                            (int)Math.Round((double)Width / (double)(m_PreviewIcon.Width)),
                            (int)Math.Round((double)Height / (double)(m_PreviewIcon.Height))
                        )
                    );
        }

        public Icon Icon
        {
            set { m_PreviewIcon = value;  Refresh(); }
            get { return m_PreviewIcon; }
        }

        public Point Hotspot
        {
            set {
                m_x = value.X;
                m_y = value.Y;
                Invalidate();
            }
            get { return new Point(m_x, m_y); }
        }

        private void IconPreview_Paint(Object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            if (Enabled == false)
            {
                g.Clear(Color.FromKnownColor(KnownColor.Control));
                return;
            }
            
            if (m_PreviewIcon == null)
            {
                g.DrawRectangle(m_BlackPen, 0, 0, Width - 1, Height - 1);
                return;
            }

            g.DrawImage(m_PreviewIcon.ToBitmap(), 0, 0, Width, Height);

            g.DrawRectangle(m_BlackPen, 0, 0, Width - 1, Height - 1);

            g.DrawRectangle(m_ActualPen,
                new Rectangle(
                (int)Math.Round(((double)Width / (double)(m_PreviewIcon.Width)) * (double)m_x),
                (int)Math.Round(((double)Height / (double)(m_PreviewIcon.Height)) * (double)m_y),
                (int)Math.Round((double)Width / (double)(m_PreviewIcon.Width)) - 1,
                (int)Math.Round((double)Height / (double)(m_PreviewIcon.Height)) - 1
                ));
        }

        private Timer m_Timer;
        private Pen m_RedPen, m_GreenPen, m_ActualPen;
        private Pen m_BlackPen;
        private Icon m_PreviewIcon;
        private int m_x, m_y;
    }
}
